﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GeoFly
{
    public static class DateAndTime
    {
       public static int[] DateNum = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        /// <summary>
        /// 根据指定的年份和Julian历日期，推算该日期对应的月份
        /// </summary>
        /// <param name="year">指定年份</param>
        /// <param name="dn">Julian历日期</param>
        /// <returns>月份值</returns>
        public static int GetMonthByDn(int year, int dn)
        {
            int result = 0;

            for (int month = 0; month < 12; month++)
            {
                if (dn <= DateAndTime.GetEachMonthDn(year, month))// DnNum[i])
                {
                    result = month;
                    break;
                }
            }
            return result + 1;
        }
        /// <summary>
        /// 根据指定的年份，推算每月月底对应的Julian历日期，如（1月31，二月59或60，...，12月365或366）
        /// </summary>
        /// <param name="year">指定年份</param>
        /// <param name="month">指定月份</param>
        /// <returns>Julian历日期指针</returns>
        public static int GetEachMonthDn(int year, int month)
        {
            bool IsLeap = CheckLeapYear(year);
            int date = 0;
            for (int i = 0; i < month; i++)			//create cumulated dn in one year;
            {
                date += DateNum[i];
                if (IsLeap && i == 1) //如果是闰年，则再加1
                    date += 1;
            }
            return date;

        }
        /// <summary>
        /// 判断输入年份是否是闰年
        /// </summary>
        /// <param name="year">需判断年份</param>
        /// <returns>true 是闰年；false不是闰年</returns>
        public static bool CheckLeapYear(int year)
        {
            if (year % 4 == 0)			//Is it a century?
            {
                if (year % 100 == 0)    // If a century, must be evenly divisible by 400.
                {
                    if (year % 400 == 0)
                        return true;
                    else
                        return false;
                }
                else
                    return true;
            }
            else
                return false;
        }
        /// <summary>
        /// 给定年、月、日，计算该日的Julian历日
        /// </summary>
        /// <param name="year">年份(4位)</param>
        /// <param name="month">月份</param>
        /// <param name="day">日期</param>
        /// <returns>Julian历日期</returns>
       public static int GetDnInYear(int year, int month, int day)
        {
            bool IsLeap = DateAndTime.CheckLeapYear(year);
            int days = 0;
            for (int i = 0; i < month - 1; i++)
            {
                if (i == 1 && IsLeap == true) //如果是闰年，2月份的天数要比平年多1天
                    days += DateNum[i] + 1;
                else
                    days += DateNum[i];
            }
            days += day;
            return days;
        }
        //the result maybe like 1998001,1998002,...1998365,
        //功能：
        //输入：
        //输出：
        public static string SetLongDateByDn(DateTime date)
        {
            int year = date.Year;
            int month = date.Month;
            int day = date.Day;
            

            int dn = GetDnInYear(year, month, day);
            string result = year+ " " + dn;

            return result;
        }
        
        
    }
}
